package org.exam.blog.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.exam.blog.domain.ArticleType;
import org.exam.blog.dto.ArticleDto;
import org.exam.blog.query.ArticleTypeQuery;
import org.exam.blog.service.IArticleTypeService;
import org.exam.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articleType")
@Api(tags = "用户提示")
public class ArticleTypeController {
    @ApiOperation(value = "查询单个接口")
    @PutMapping
    public JsonResult add(@RequestBody ArticleType articleType) {
        try {
            if (articleType.getId() == null) {
                iArticleTypeService.add(articleType);
                return JsonResult.success();
            } else {
                iArticleTypeService.update(articleType);
                return JsonResult.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @Autowired
    private IArticleTypeService iArticleTypeService;

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable Long id) {
        try {
            iArticleTypeService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @GetMapping("/{id}")
    public ArticleType get(Long id) {
        return iArticleTypeService.get(id);
    }

    @GetMapping
    public List<ArticleType> getAll() {
        return iArticleTypeService.getAll();
    }

    @PatchMapping
    public JsonResult patchDelete(@RequestBody Long[] ids) {
        try {
            iArticleTypeService.patchDelete(ids);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping
    public PageInfo<ArticleType> queryPage(@RequestBody ArticleTypeQuery articleTypeQuery) {
        return iArticleTypeService.queryPage(articleTypeQuery);
    }

    @GetMapping("/typeTree")
    public List<ArticleType> typeTree() {
        return iArticleTypeService.typeTree();


    }
    @GetMapping("/countByArticleType")
    public List<ArticleDto> get1 (){
        return iArticleTypeService.get1();
    }
    @GetMapping("/getCountByTime")
    public List<ArticleDto> get2 (){
        return iArticleTypeService.get2();
    }
}
